//
//  HomeViewController.swift
//  KkmhSwift
//
//  Created by tenltrs on 2018/10/26.
//  Copyright © 2018 Tenltrs. All rights reserved.
//

import UIKit
import LocalAuthentication

class HomeViewController: BaseViewController,SGPageContentScrollViewDelegate,TopSwitchViewDelegate{

    var contentView : SGPageContentScrollView!
    
    var vouchersBtn : UIImageView!
    
    var titleView : TopSwitchView!
    
    var activityModel : HomeActivityModel?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initNav()
        
        initData()
        
        initContentView()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

    }
    
    func initNav() {
        
        //创建左侧按钮
        vouchersBtn = UIImageView.init().then({ (make) in
            make.size=CGSize(width: 75.0/55*18, height: 18)
            make.isUserInteractionEnabled=true
        })

        let vouchersBar = UIBarButtonItem.init(customView: vouchersBtn)
        navigationItem.leftBarButtonItem=vouchersBar
        

        titleView = TopSwitchView.init(frame: CGRect(x: 0, y: 0, width: 91, height: 28), backgroundImage: UIImage(named: "ic_common_nav_tab_bg_91x28_")!, sliderImage: UIImage(named: "ic_common_nav_cute")!, titles: ["关注","热门"], fontColor: UIColor.NavColor, selectFontColor: UIColor(hexString: "#45240C")!, defaultSelect: 1)
        titleView.delegate=self
        navigationItem.titleView=titleView
        
        
        //创建右侧按钮
        let searchBtn = UIViewTool.mas_button(title: nil, image: "ic_home_nav_search_normal_18x17_", backgroundImage: nil, titleColor: nil, fontSize: nil)
        searchBtn.addTarget(self, action: #selector(searchAction), for: UIControl.Event.touchUpInside)
        searchBtn.size=CGSize(width: 36, height: 40)
        let searchBar = UIBarButtonItem.init(customView: searchBtn)
        
        let historyBtn = UIViewTool.mas_button(title: nil, image: "ic_history_18x18_", backgroundImage: nil, titleColor: nil, fontSize: nil)
        historyBtn.size=CGSize(width: 36, height: 40)
        historyBtn.addTarget(self, action: #selector(historyAction), for: UIControl.Event.touchUpInside)
        let historyBar = UIBarButtonItem.init(customView: historyBtn)
        
        self.navigationItem.rightBarButtonItems=[searchBar,historyBar]
    }
    
    
    func initData(){
        weak var ws = self
        RequestManager.GET(url: RequestUrls.business_activities_get, parameter: ["is_new_device":false], target: self, isLoad: false, success: { (result) in
            if result.success{

                ws?.activityModel=HomeActivityModel.mj_object(withKeyValues: result.data!)
//                ws?.vouchersBtn.sd_setImage(with: URL(string: ws?.activityModel?.home_left_top_icon?.image_url ?? ""), for: UIControl.State.normal, completed: { (image, error, type, url) in
//                })
                
                ws?.vouchersBtn.ts_loadImage(url: ws?.activityModel?.home_left_top_icon?.image_url, completionHandler: { (image, error, type, url) in
                    if(image != nil){
                        let newImage = image!.byCrop(to: CGRect(x: 20, y: 30, width: 75, height: 55))
                        ws?.vouchersBtn.image=newImage
                    }

                })
            }
        }, failure: nil)
    }
    
    
    func initContentView() {
        contentView=SGPageContentScrollView.init(frame: view.bounds, parentVC: self, childVCs: [HomeFocusController(),HomeHotController()])
        contentView.delegatePageContentScrollView=self
        contentView.isAnimated=true
        contentView.setPageContentScrollViewCurrentIndex(1)
        view.addSubview(contentView!)
        
    }
    
   
    @objc func searchAction(){
        
        
    }
    
    @objc func historyAction(){
        
        
    }
    
    // MARK: -- 协议代理
    func pageContentScrollView(_ pageContentScrollView: SGPageContentScrollView!, index: Int) {
        
        titleView.on=index==1
    }
    
    func selectActionWithIndex(index: NSInteger) {
        contentView.setPageContentScrollViewCurrentIndex(index)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        
        super.viewWillDisappear(animated)

    }

}
